package org.micode.demo.web.controller;

import org.micode.common.helper.I18nMsgHelper;
import org.micode.common.validater.InsertGroup;
import org.micode.common.validater.UpdateGroup;
import org.micode.demo.web.base.BaseWebAdminController;
import org.micode.demo.web.service.SysUserService;
import org.micode.common.annotation.NoNeedLogin;
import org.micode.common.annotation.PermissionsFlag;
import org.micode.common.annotation.SysLog;
import org.micode.common.domain.ApiResult;
import org.micode.common.domain.DataPage;
import org.micode.common.domain.vo.KeyVO;
import org.micode.common.enums.OperationModeEnum;
import org.micode.common.utils.StringExtUtils;
import org.micode.demo.web.vo.DisableUpdateVO;
import org.micode.libs.dao.helper.UserHelper;
import org.micode.demo.db.domain.dto.SysUserDTO;
import org.micode.demo.db.domain.query.SysUserQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 企业用户信息(company_user) 的APP控制类
 * <p>
 * author: wutianbin
 */
@RestController
@RequestMapping("/web-admin/sys/user")
@Slf4j
@Validated
public class SysUserController
        extends BaseWebAdminController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 分页查询系统用户列表数据
     *
     * @param dataQuery 查询条件，如果条件中不包含page对象，则系统会指定一个默认的page对象。
     * @return 返回查询结果记录集
     */
    @PostMapping("/list")
    @PermissionsFlag("sys:user:list")
    public ApiResult<DataPage<SysUserDTO>> list(
            @RequestBody SysUserQuery dataQuery) {
        log.debug(I18nMsgHelper.get("api.call.sysUser.list") + " == {}", dataQuery);
        return listForQuery(sysUserService::list, dataQuery);
    }

    /**
     * 按主键查询系统用户详细信息
     *
     * @param userId 要查询的主键值, 例如: {"key": "1"}
     * @return 主键对应的对象
     */
    @PostMapping("/info")
    @PermissionsFlag("sys:user:info")
    public ApiResult<SysUserDTO> info(
            @RequestBody @Validated KeyVO<Long> userId) {
        log.debug(I18nMsgHelper.get("api.call.sysUser.info") + " == {}", userId.getKey());
        return invokeMethod(sysUserService::info, userId);
    }

    /**
     * 新增系统用户记录
     *
     * @param sysUser 要新增的对象
     * @return 新增成功后的对象
     */
    @SysLog(value = "{api.syslog.sysUser.insert}", operationMode = OperationModeEnum.INSERT)
    @PostMapping("/insert")
    @PermissionsFlag("sys:user:insert")
    public ApiResult<SysUserDTO> insert(
            @RequestBody @Validated(InsertGroup.class) SysUserDTO sysUser) {
        log.debug(I18nMsgHelper.get("api.call.sysUser.insert") + " == {}", sysUser);
        try {
            UserHelper.checkPassword(sysUser.getPassword());
            UserHelper.checkLoginName(sysUser.getLoginName());
        } catch (Exception e) {
            log.warn(e.getMessage());
            return returnFail(e.getMessage());
        }
        return insertTheDTO(sysUserService::insert, sysUser);
    }

    /**
     * 按主键更新系统用户信息
     *
     * @param sysUser 要更新的信息，userId必传，其他值是要更新的数据
     * @return 更新处理影响的记录数
     */
    @SysLog(value = "{api.syslog.sysUser.update}", operationMode = OperationModeEnum.UPDATE)
    @PostMapping("/update")
    @PermissionsFlag("sys:user:update")
    public ApiResult<Integer> update(
            @RequestBody @Validated(UpdateGroup.class) SysUserDTO sysUser) {
        log.debug(I18nMsgHelper.get("api.call.sysUser.update") + " == {}", sysUser);
        try {
            UserHelper.checkLoginName(sysUser.getLoginName());
        } catch (Exception e) {
            log.warn(e.getMessage());
            return returnFail(e.getMessage());
        }
        return updateTheDTO(sysUserService::update, sysUser);
    }

    /**
     * 按主键删除系统用户信息
     *
     * @param userIds 要删除的记录ID集，例如: ["1", "2"]
     * @return 删除处理影响的记录数
     */
    @SysLog(value = "{api.syslog.sysUser.delete}", operationMode = OperationModeEnum.DELETE)
    @PostMapping("/delete")
    @PermissionsFlag("sys:user:delete")
    public ApiResult<Integer> delete(
            @RequestBody List<Long> userIds) {
        log.debug(I18nMsgHelper.get("api.call.sysUser.delete") + " == {}", StringExtUtils.idsString(userIds));
        return deleteForIds(sysUserService::delete, userIds);
    }

    /**
     * 禁用处理，禁用/启用都是这个接口处理
     *
     * @param disableUpdateVO 禁用信息
     * @return 处理影响的记录数
     */
    @SysLog(value = "{api.syslog.sysUser.disable}", operationMode = OperationModeEnum.UPDATE)
    @PostMapping("/disable")
    @PermissionsFlag("sys:user:update")
    public ApiResult<Integer> disable(
            @RequestBody @Validated(UpdateGroup.class) DisableUpdateVO<Long> disableUpdateVO) {
        log.debug(I18nMsgHelper.get("api.call.sysUser.disable") + " == {}", disableUpdateVO);
        SysUserDTO updateDTO = new SysUserDTO();
        updateDTO.setUserId(disableUpdateVO.getId());
        updateDTO.setDisableFlag(disableUpdateVO.getDisableFlag());
        return updateTheDTO(sysUserService::update, updateDTO);
    }

    /**
     * 重置密码
     *
     * @param userKey
     * @return
     */
    @SysLog(value = "{api.syslog.sysUser.resetPassword}", operationMode = OperationModeEnum.UPDATE)
    @PostMapping("/resetPassword")
    @PermissionsFlag("sys:user:update")
    public ApiResult<Integer> resetPassword(
            @RequestBody @Validated KeyVO<Long> userKey) {
        log.debug(I18nMsgHelper.get("api.call.sysUser.resetPassword") + " == {}", userKey);
        SysUserDTO userDTO = new SysUserDTO();
        userDTO.setUserId(userKey.getKey());
        fillLoginFields(userDTO);
        return invokeMethod(sysUserService::resetPassword, userDTO);
    }

    /**
     * 重置登录次数
     *
     * @param userKey
     * @return
     */
    @SysLog(value = "{api.syslog.sysUser.resetLoginNum}", operationMode = OperationModeEnum.UPDATE)
    @PostMapping("/resetLoginNum")
    @PermissionsFlag("sys:user:update")
    public ApiResult<Integer> resetLoginNum(
            @RequestBody @Validated KeyVO<Long> userKey) {
        log.debug(I18nMsgHelper.get("api.call.sysUser.resetLoginNum") + " == {}", userKey);
        SysUserDTO userDTO = new SysUserDTO();
        userDTO.setUserId(userKey.getKey());
        fillLoginFields(userDTO);
        return invokeMethod(sysUserService::resetLoginNum, userDTO);
    }

}
